package cn.itsource.order.controller;

import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.RedisUtils;
import cn.itsource.order.service.IProductOrderService;
import cn.itsource.user.domain.User;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@RequestMapping("/order")
public class ProductOrderController {
    /*
        let param={
				    product_id:this.product.id,
					pay_way:'aliPay',
					address_id:1,
				}
     */
    @Autowired
    private IProductOrderService productOrderService;

    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody HashMap<String,String> map, HttpServletRequest request){
        String redisKey = request.getHeader("U-TOKEN");
        String userJson = RedisUtils.INSTANCE.get(redisKey);
        User user = JSONObject.parseObject(userJson, User.class);

        String payResult = productOrderService.createOrder(map, user);


        return  AjaxResult.me().setResultObj(payResult);
    }
}
